#encoding=utf8
'''
Created on 2012-10-18

@author: Administrator
'''
from dispatch.Processor import Processor
from pub import Func, UserDict, RoomDict;
from pub.Config import Config

class RISOClass(Processor):
    '''
    指令：RISO(room idiom self out)
    备注：玩家自动退出房间
    格式：c-s:cmd#cmdId#roomId#userId
     s-c:cmd#cmdId#roomId#masterId#outId#outuserNickName
    
    '''
    def __init__(self,):
        Processor.__init__(self,)
        self.__roomId = 0;
        self.__userId = 0;
        
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 2:      
            self.__roomId = Func.strToInt(cmdContent[0], Config.I_SYS_ZERO);
            self.__userId = Func.strToInt(cmdContent[1], Config.I_SYS_ZERO);
            return True;        
        return False;
    def outData(self,):
        room = RoomDict.getRoomFromDictById(self.__roomId);
        if not room:
            return False;
        userOut = UserDict.getUserFromUserDictById(self.__userId);
        if not userOut:
            return False;
        outNickName = userOut.get_user_nick_name();        
#        owner = room.get_room_owner();
#        if not owner:
#            return False;
#        ownerUserId = owner.get_user_id(); 
        RoomDict.delUserToRoom(userOut, self.__roomId);
        owner = room.get_room_owner();
        if not owner:
            ownerUserId = owner.get_user_id();
        
        if not room.get_user_list():#房间时没人
            room.reset();
            
        sendData = [self.__roomId, ownerUserId, self.__userId, outNickName];
        sendStr = self.setPackage(sendData);
        self._sockObj.sendLine(sendStr);
        RoomDict.sendInfoToRoomAllUser(roomObj=room, cmd="", msg=sendStr, isPackage=False);
        return True;
